package com.example.test8326;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class rwfile extends AppCompatActivity {
    private Button bt_w;
    private Button bt_r;
    private CheckBox appendBox;
    private EditText editText;
    private TextView textView;
    private TextView textView_show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rwfile);
        bt_w = findViewById(R.id.bt_w);
        bt_r = findViewById(R.id.bt_r);
        editText = findViewById(R.id.ed_text);
        appendBox = findViewById(R.id.box_append);
        textView = findViewById(R.id.sys_message);
        textView_show = findViewById(R.id.showMessage);

        bt_w.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(getApplication(), "点击了这个", Toast.LENGTH_SHORT).show();
                FileOutputStream fos = null;
                try {
                    if (appendBox.isChecked()) {
                        fos = openFileOutput("test1.txt", Context.MODE_APPEND);
                    } else {
                        fos = openFileOutput("test1.txt", Context.MODE_PRIVATE);
                    }

                    String text = editText.getText().toString();

                    fos.write(text.getBytes());
                    textView.setText("文件写入成功，写入长度：" + text.length());
                    editText.setText("");
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (fos != null) {
                        try {
                            fos.flush();
                            fos.close();

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

        bt_r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(getApplication(), "点击了那个", Toast.LENGTH_SHORT).show();
                textView_show.setText("");
                FileInputStream fis = null;
                try {
                    fis = openFileInput("test1.txt");
                    if (fis.available() == 0) {
                        return;
                    }
                    byte[] readBytes = new byte[fis.available()];
                    while (fis.read(readBytes) != -1) {
                    }
                    String text = new String(readBytes);
                    textView_show.setText(text);
                    textView.setText("文件写入成功，写入长度：" + text.length());

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }
}